package com.syz.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syz.mall.mapper.CategoryMapper;
import com.syz.mall.pojo.Category;
import com.syz.mall.pojo.Product;
import com.syz.mall.pojo.query.CategoryQuery;
import com.syz.mall.service.ICategoryService;
import com.syz.mall.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public LayUITableResult selectByPage(CategoryQuery categoryQuery) {
        PageHelper.startPage(categoryQuery.getPage(), categoryQuery.getLimit());
        List<Category> list = categoryMapper.selectAll(categoryQuery);
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();
        return LayUITableResult.ok((int)totalCount, list);
    }
    @Override
    public boolean deleteAll(Integer[] ids) {
        int count = categoryMapper.deleteAll(ids);
        return count == ids.length;
    }

    @Override
    public boolean deleteById(Integer id) {
        int count = categoryMapper.deleteByPrimaryKey(id);
        return count == 1;
    }

    @Override
    public boolean add(Category category) {
        int count = categoryMapper.insertSelective(category);
        return count == 1;
    }

    @Override
    public Category selectById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Category> selectAllTop() {
        return categoryMapper.selectAllTop();
    }

    @Override
    public List<Category> selectAllSec(Integer topId) {
        return categoryMapper.selectAllSec(topId);
    }

    @Override
    public Category selectTopById(Integer id) {
        return categoryMapper.selectTopBuId(id);
    }
    @Override
    public boolean update(Category category) {
        int count =categoryMapper.updateByPrimaryKeySelective(category);
        return count==1;
    }

    @Override
    public boolean changeStatus(Integer id, Integer status) {
        int count =categoryMapper.changeStatus(id,status);
        return count==1;
    }
}
